package trong.gold.soft.com.view;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.midlet.MIDletStateChangeException;

import trong.gold.soft.com.model.Resources;
import trong.gold.soft.com.model.createImg;
import trong.gold.soft.com.model.drawBackgr;
import trong.gold.soft.com.model.font.VietnameseFont;
import trong.gold.soft.com.main.mTetrisMidlet;
import trong.gold.soft.com.view.game.GridCanvas;
import trong.gold.soft.com.view.game.teamTetris;
public class menuTetris extends GameCanvas{
	private Image backgr,indexk,kimk;
	private Graphics g;
	private String indexName = "khung.png";
	private String kkName = "kim-cuong.png";
	private String[] mang;
	private int curr=0;
	private int latx=0,laty=0;
	private mTetrisMidlet midlet;
	private GridCanvas grid;
	private drawBackgr back;
	public menuTetris(mTetrisMidlet midlet) {
		super(false);
		this.midlet = midlet;
		back = new drawBackgr();
		this.g = this.getGraphics();
		setFullScreenMode(true);
		backgr = createImg.createImage("/com/image/" + Resources.backgrName);
		indexk = createImg.createImage("/com/image/" + indexName);
		kimk = createImg.createImage("/com/image/" + kkName);
		Draw();
	}
	private void Draw(){
		//g.drawImage(backgr, 0, 0, Resources.TOP_LEFT_ANCHOR);
		back.drawbackgr(this.getWidth(), this.getHeight(), 1, backgr, g, 40, 60);
		mang = new String[5];
		mang[0]="Chơi ngay";
		mang[1]="Thành tích";
		mang[2]="Cài đặt";
		mang[3]="Gửi tặng";
		mang[4]="Game khác";
		for(int i=0;i<5;i++){
			g.drawImage(indexk, 10, this.getHeight()/3 + i*indexk.getHeight() + 5, Resources.TOP_LEFT_ANCHOR);
			VietnameseFont.BlackBold.DrawString(g, mang[i],indexk.getWidth()/2 + VietnameseFont.BlackBold.getWidth(mang[i])/2, this.getHeight()/3 + i*indexk.getHeight() + 5 + + VietnameseFont.BlackBold.getHeight()/2, Graphics.HCENTER);
		}
		g.drawImage(kimk, indexk.getWidth()/5, this.getHeight()/3, Resources.TOP_LEFT_ANCHOR);
		VietnameseFont.BlackBold.DrawString(g, "Chọn",this.getWidth()/2, this.getHeight() - VietnameseFont.WhiteBold.getHeight(), Graphics.HCENTER);
		VietnameseFont.BlackBold.DrawString(g, "Thoát",this.getWidth() - VietnameseFont.WhiteBold.getWidth("Thoát"), this.getHeight() - VietnameseFont.WhiteBold.getHeight(), Graphics.HCENTER);
		flushGraphics();
	}
	private void redraw(int i){
		
		g.drawImage(indexk, 10, this.getHeight()/3 + i*indexk.getHeight() + 5, Resources.TOP_LEFT_ANCHOR);
		VietnameseFont.BlackBold.DrawString(g, mang[i],indexk.getWidth()/2 + VietnameseFont.BlackBold.getWidth(mang[i])/2, this.getHeight()/3 + i*indexk.getHeight() + 5 + + VietnameseFont.BlackBold.getHeight()/2, Graphics.HCENTER);
		g.drawImage(kimk, indexk.getWidth()/5, this.getHeight()/3 + i*indexk.getHeight(), Resources.TOP_LEFT_ANCHOR);
		flushGraphics();
	}
	private void clear(int i){
		g.drawImage(indexk, 10, this.getHeight()/3 + i*indexk.getHeight() + 5, Resources.TOP_LEFT_ANCHOR);
		VietnameseFont.BlackBold.DrawString(g, mang[i],indexk.getWidth()/2 + VietnameseFont.BlackBold.getWidth(mang[i])/2, this.getHeight()/3 + i*indexk.getHeight() + 5 + + VietnameseFont.BlackBold.getHeight()/2, Graphics.HCENTER);
		flushGraphics();
	}
	
	private void menuchange(int i){
		curr += i + mang.length;
        curr = curr % mang.length;
	}
	public void pointerPressed(int x,int y){
			if(!hasPointerEvents()){
			return;
		}
		latx=x;
		laty=y;
		int tam=0;
		for(int i=0;i<mang.length;i++){
			for(int j=10;j<10 + indexk.getWidth();j++)
				for(int k=(this.getHeight()/3 + i*indexk.getHeight() + 5);k<(this.getHeight()/3 + i*indexk.getHeight() + 5) + indexk.getHeight();k++){
					if(j==latx && k==laty){
						System.out.println(i);
						clear(curr);
						menuchange(i-curr);
						redraw(curr);
						tam=i;
					}
				}
		}
		for(int i=this.getWidth()/2;i<=this.getWidth()/2 + VietnameseFont.BlackBold.getWidth("Chọn");i++)
			for(int j=this.getHeight() - VietnameseFont.BlackBold.getHeight();j<=this.getHeight();j++){
				if(i==latx && j==laty){
					switchcase(tam);
				}
			}
		for(int i=this.getWidth() - VietnameseFont.BlackBold.getWidth("Thoát");i<=this.getWidth();i++)
			for(int j=this.getHeight() - VietnameseFont.BlackBold.getHeight();j<=this.getHeight();j++){
				if(i==latx && j==laty){
					setnull();
					midlet.notifyDestroyed();
				}
			}
		switchcase(tam);
	}
	private void switchcase(int x){
		switch (x) {
    	case 0:
    		if(grid==null){
    			grid = new GridCanvas(midlet);
    			midlet.switchState(this,grid);
    		}
    		break;
    	case 1:
    		midlet.switchState(this,new thanhtichTetris(midlet));
    		break;
    	case 2:
    		midlet.switchState(this,new settingTetris(midlet));
    		break;
    	case 3:
    		midlet.switchState(this,new guitangTetris(midlet));
    		break;
    	case 4:
    		setnull();
    		Runtime.getRuntime().gc();
		try {
			midlet.platformRequest("http://mbin.vn/free/index.jsp");
			
		} catch (ConnectionNotFoundException e) {
			e.printStackTrace();
		}
		midlet.notifyDestroyed();
    		break;
	}
	}
	public void keyPressed(int keyCode) {
		
		switch (keyCode) {
		case -1:
			System.out.println("up");
			clear(curr);
			menuchange(-1);
			redraw(curr);
			break;
		case -2:
			System.out.println("down");
			clear(curr);
			menuchange(1);
			redraw(curr);
			break;
		case -5:
			setnull();
			Runtime.getRuntime().gc();
			switchcase(curr);
			break;
		case -7:
			setnull();
			midlet.notifyDestroyed();
			break;
	}
	}
	private void setnull(){
		backgr=null;
		indexk=null;
		kimk=null;
		g = null;
		indexName=null;
		kkName=null;
		mang=null;
		back=null;
	}
}